﻿#include "BlinkBorderWindow.h"
#include <QPainter>
#include <QApplication>

BlinkBorderWindow::BlinkBorderWindow(const QRect &rect, QWidget *parent, QColor color)
    : QWidget(parent)
{
    // 设置窗口位置和大小
    setGeometry(rect);

    // 透明背景 + 无边框
    setWindowFlags(Qt::FramelessWindowHint | Qt::Tool | Qt::WindowStaysOnTopHint);
    setAttribute(Qt::WA_TranslucentBackground, true);

    // 初始边框颜色
    m_borderColor = color;

    // 闪烁定时器
    m_blinkTimer.setInterval(200); // 300ms 切换一次颜色
    connect(&m_blinkTimer, &QTimer::timeout, this, &BlinkBorderWindow::toggleBlink);
    m_blinkTimer.start();

    // 2 秒后销毁
    QTimer::singleShot(2000, this, &QWidget::close);
}

void BlinkBorderWindow::toggleBlink()
{
    m_visibleBorder = !m_visibleBorder;
    update();
}

void BlinkBorderWindow::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    p.setRenderHint(QPainter::Antialiasing);

    if (m_visibleBorder) {
        uint8_t w = 6;
        QPen pen(m_borderColor, w);
        p.setPen(pen);
        p.drawRect(rect().adjusted(w/2, w/2, -w/2, -w/2));
    }
}
